package com.xinke.module.system.service.user;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.user.*;
import com.xinke.module.system.model.entity.user.UserPostDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.user.UserPostMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 用户和岗位关联 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class UserPostServiceImpl implements UserPostService {

    @Resource
    private UserPostMapper userPostMapper;

    @Override
    public Long createUserPost(UserPostSaveReqVO createReqVO) {
        // 插入
        UserPostDO userPost = BeanUtils.toBean(createReqVO, UserPostDO.class);
        userPostMapper.insert(userPost);
        // 返回
        return userPost.getUserPostId();
    }

    @Override
    public void updateUserPost(UserPostSaveReqVO updateReqVO) {
        // 校验存在
        validateUserPostExists(updateReqVO.getUserPostId());
        // 更新
        UserPostDO updateObj = BeanUtils.toBean(updateReqVO, UserPostDO.class);
        userPostMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserPost(Long userPostId) {
        // 校验存在
        validateUserPostExists(userPostId);
        // 删除
        userPostMapper.deleteById(userPostId);
    }

    private void validateUserPostExists(Long userPostId) {
        if (userPostMapper.selectById(userPostId) == null) {
            throw exception(null);
        }
    }

    @Override
    public UserPostDO getUserPost(Long userPostId) {
        return userPostMapper.selectById(userPostId);
    }

    @Override
    public PageResult<UserPostDO> getUserPostPage(UserPostPageReqVO pageReqVO) {
        return userPostMapper.selectPage(pageReqVO);
    }

    @Override
    public boolean insertBatch(List<UserPostDO> userPostDOList) {
        return userPostMapper.insertBatch(userPostDOList);
    }

}